package com.gin.kotlin.no_gay_alliance.base

import android.content.Context
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.GlideUtils.Companion.CACHE_DIR
import com.gin.kotlin.no_gay_alliance.interceptor.Block404Interceptor
import com.gin.kotlin.no_gay_alliance.interceptor.LoggingInterceptor
import okhttp3.Cache
import okhttp3.OkHttpClient
import java.io.File
import java.io.InputStream

@GlideModule
class GlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        super.registerComponents(context, glide, registry)

        val client = OkHttpClient.Builder()
            .addInterceptor(Block404Interceptor())
            .addInterceptor(LoggingInterceptor())
            // 配置缓存目录
            .cache(Cache(File(context.cacheDir, CACHE_DIR), 400 * App.MB))
            .build()
        Log.d(TAG, "[Glide] 注册OkHttp客户端")
        registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(client))
    }

    companion object {
        private val TAG = GlideModule::class.java.simpleName
        const val CROSS_FADE = 700
    }
}